<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: index == options1Active }"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <!-- 
            点击排序项的时候，得到当前的min max 的积分值 执行查询商品的方法
            {显示内容：'500-1000',min:500,max:1000}
         -->
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: item.isShowActive }"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="goodsListShow" />
      <div class="modeGo">
        <div class="modeGo-in">
          {{ isLoadingMode ? "" : "LO" }}
        </div>
      </div>
    </div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { GoodsApi } from "../request/api";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";
export default {
  data() {
    return {
      // 栏目 0全部 1精品 2热门
      did: 0,
      // 类型 0全部 1实体 2虚拟
      type: 0,
      // 最小积分值
      min: 0,
      // 最大积分值
      max: 0,
      // 搜索关键字
      keyword: "",
      // 用于展示的Arr
      goodsListShow: [],
      GoodsArr: [],
      //排序项
      options1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      options1Active: 0,
      options2: [
        { txt: "全部", type: 0, isShowActive: true },
        { txt: "实物礼品", type: 1, isShowActive: false },
        { txt: "虚拟礼品", type: 2, isShowActive: false },
      ],
      page: 1,
      size: 8,
      isLoading: false,
      isLoadingMode: false,
    };
  },
  components: {
    Crumb,
    List,
  },
  created() {
    let keyword = this.$route.query.keyword;
    if (keyword) {
      this.keyword = keyword;
      this.goodsSearch();
    } else {
      this.goodsSearch();
    }
  },
  mounted() {
    // 监听滚动事件
    // window.addEventListener("scroll", 处理函数);
    window.addEventListener("scroll", this.scrollFn);
  },
  // 销毁之前删除监听滚动事件
  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollFn);
  },
  methods: {
    async goodsSearch() {
      let res = await GoodsApi({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      this.GoodsArr = res.data;

      // 默认显示第一页 8个商品
      this.goodsListShow = this.GoodsArr.filter((item, index) => index < 8);

      // 初始化
      this.page = 1;
      this.size = 8;
      // 数据是否达到底部
      if (this.GoodsArr.length == this.goodsListShow.length) {
        this.isLoadingMode = true;
        return;
      } else {
        this.isLoadingMode = false;
      }
    },
    options1Fn(idx, min, max) {
      this.options1Active = idx;
      this.min = min;
      this.max = max;
      this.goodsSearch();
    },
    options2Fn(idx, type) {
      // 排他
      this.options2.forEach((item) => (item.isShowActive = false));
      this.options2[idx].isShowActive = true;
      this.type = type;
      this.goodsSearch();
    },
    scrollFn() {
      // console.log(
      //   "滚动了",
      //   getScrollTop() + getClientHeight() + getScrollHeight()
      // );

      // 当可视高度+滚动距离 > 文档高度 时 准备加载下一页数据
      if (getScrollTop() + getClientHeight() >= getScrollHeight() - 300) {
        if (this.GoodsArr.length == this.goodsListShow.length) {
          this.isLoadingMode = true;
          return;
        } else {
          this.isLoadingMode = false;
        }

        if (this.isLoading == false) {
          this.isLoading = true;
          this.page++;
          console.log("加载下一页");
          // 使用当时器模拟加载速度时间
          setTimeout(() => {
            for (
              let i = (this.page - 1) * this.size;
              i < this.page * this.size;
              i++
            ) {
              console.log("for", this.page, i, this.size);
              this.GoodsArr[i] && this.goodsListShow.push(this.GoodsArr[i]);
            }
            this.isLoading = false;
          }, 1000);
        }
      }
    },
  },
  watch: {
    "$route.query.keyword": {
      handler(n, o) {
        this.keyword = n;
        this.goodsSearch();
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";
@base-color: #0a328e;

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
.modeGo {
  width: 100%;
  margin-top: 20px;
}
.modeGo-in {
  font-size: 30px;
  margin: 0 auto;
  width: 30px;
  animation: dong 0.3s infinite;
}
@keyframes dong {
  0% {
    transform: rotateZ(0);
  }
  12% {
    transform: rotateZ(45deg);
  }
  25% {
    transform: rotateZ(90deg);
  }
  37% {
    transform: rotateZ(135deg);
  }
  50% {
    transform: rotateZ(180deg);
  }
  62% {
    transform: rotateZ(225deg);
  }
  75% {
    transform: rotateZ(270deg);
  }
  87% {
    transform: rotateZ(315deg);
  }
  100% {
    transform: rotateZ(360deg);
  }
}
</style>